package com.platform.modules.mall.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.utils.Constant;
import com.platform.modules.mall.dao.MallAccessStatisticsDao;
import com.platform.modules.mall.entity.MallAccessStatisticsEntity;
import com.platform.modules.mall.service.MallAccessStatisticsService;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service("mallAccessStatisticsService")
public class MallAccessStatisticsServiceImpl extends ServiceImpl<MallAccessStatisticsDao, MallAccessStatisticsEntity> implements MallAccessStatisticsService {


    @Override
    public Map<String, Object> pageStatistics(Map<String, Object> params) {
        //如果没传日期时间段，查近三十天
        defaultDate(params);

        Map<String, Object> resultMap = new HashMap<>();
        List<Integer> pageSeriesData = new ArrayList<>();
        List<String> pageTitleData = new ArrayList<>();
        List<MallAccessStatisticsEntity> statisticsData = baseMapper.pageStatistics(params);
        for (MallAccessStatisticsEntity item : statisticsData) {
            pageSeriesData.add(item.getNum());
            pageTitleData.add(item.getName());
        }


        resultMap.put("pageSeriesData", pageSeriesData);
        resultMap.put("pageTitleData", pageTitleData);
        return resultMap;
    }

    @Override
    public Map<String, Object> goodsAccessStatistics(Map<String, Object> params) {
        //如果没传日期时间段，查近三十天
        defaultDate(params);

        Map<String, Object> resultMap = new HashMap<>();
        List<Integer> pageSeriesData = new ArrayList<>();
        List<String> pageTitleData = new ArrayList<>();
        List<MallAccessStatisticsEntity> statisticsData = baseMapper.goodsAccessStatistics(params);
        for (MallAccessStatisticsEntity item : statisticsData) {
            pageSeriesData.add(item.getNum());
            pageTitleData.add(item.getName());
        }


        resultMap.put("pageSeriesData", pageSeriesData);
        resultMap.put("pageTitleData", pageTitleData);
        return resultMap;
    }

    @Override
    public Map<String, Object> goodsBuyStatistics(Map<String, Object> params) {
        //如果没传日期时间段，查近三十天
        defaultDate(params);

        Map<String, Object> resultMap = new HashMap<>();
        List<Integer> pageSeriesData = new ArrayList<>();
        List<String> pageTitleData = new ArrayList<>();
        List<MallAccessStatisticsEntity> statisticsData = baseMapper.goodsBuyStatistics(params);
        for (MallAccessStatisticsEntity item : statisticsData) {
            pageSeriesData.add(item.getNum());
            pageTitleData.add(item.getName());
        }


        resultMap.put("pageSeriesData", pageSeriesData);
        resultMap.put("pageTitleData", pageTitleData);
        return resultMap;

    }

    @Override
    public Map<String, Object> goodsRefundStatistics(Map<String, Object> params) {
        //如果没传日期时间段，查近三十天
        defaultDate(params);

        Map<String, Object> resultMap = new HashMap<>();
        List<Integer> pageSeriesData = new ArrayList<>();
        List<String> pageTitleData = new ArrayList<>();
        List<MallAccessStatisticsEntity> statisticsData = baseMapper.goodsRefundStatistics(params);
        for (MallAccessStatisticsEntity item : statisticsData) {
            pageSeriesData.add(item.getNum());
            pageTitleData.add(item.getName());
        }
        resultMap.put("pageSeriesData", pageSeriesData);
        resultMap.put("pageTitleData", pageTitleData);
        return resultMap;
    }

    private Map<String, Object> defaultDate(Map<String, Object> params) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendarStart = Calendar.getInstance();
        Calendar calendarEnd = Calendar.getInstance();
        if (!params.containsKey("addTimeStart")) {
            calendarStart.add(Calendar.DATE, -30);
            params.put("addTimeStart", sdf.format(calendarStart.getTime())+" 00:00:00");
            params.put("addTimeEnd", sdf.format(calendarEnd.getTime())+" 23:59:59");
        }else{
            //加入小时 零点到23:59:59 防止查不出数据
            params.put("addTimeStart",params.get("addTimeStart").toString()+" 00:00:00");
            params.put("addTimeEnd",params.get("addTimeEnd").toString()+" 23:59:59");
        }
        return params;
    }


    @Override
    public int userTotalStatistics() {
        int userTotal = baseMapper.userTotalStatistics();
        return userTotal;
    }

    @Override
    public Map<String, Object> userAddStatistics(Map<String, Object> params) {
        Map<String, Object> resultMap = new HashMap<>();
        List<Integer> pageSeriesData = new ArrayList<>();
        List<String> pageTitleData = new ArrayList<>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendarStart = Calendar.getInstance();
        Calendar calendarEnd = Calendar.getInstance();
        if (!params.containsKey("addTimeStart")) {
            calendarStart.add(Calendar.DATE, -30);
            params.put("addTimeStart", sdf.format(calendarStart.getTime()));
            params.put("addTimeEnd", sdf.format(calendarEnd.getTime()));
        } else {
            try {
                calendarStart.setTime(sdf.parse(params.get("addTimeStart").toString()));
                calendarEnd.setTime(sdf.parse(params.get("addTimeEnd").toString()));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        while (!sdf.format(calendarStart.getTime()).equals(sdf.format(calendarEnd.getTime()))) {
            pageTitleData.add(sdf.format(calendarStart.getTime()));
            calendarStart.add(Calendar.DATE, 1);
        }
        pageTitleData.add(sdf.format(calendarEnd.getTime()));

        List<MallAccessStatisticsEntity> statisticsData = baseMapper.userAddStatistics(params);

        for (int d = 0; d < pageTitleData.size(); d++) {
            pageSeriesData.add(Constant.ZERO);
            for (int i = 0; i < statisticsData.size(); i++) {
                if (pageTitleData.get(d).equals(statisticsData.get(i).getDate())) {
                    pageSeriesData.set(d, statisticsData.get(i).getNum());
                }
            }
        }

        resultMap.put("pageSeriesData", pageSeriesData);
        resultMap.put("pageTitleData", pageTitleData);
        return resultMap;
    }

    @Override
    public Map<String, Object> userActiveStatistics() {
        Map<String, Object> resultMap = new HashMap<>();
        //获取用户总数
        int userTotal = userTotalStatistics();
        //获取用户不活跃数
        int userNoActiveNum = baseMapper.userNoActiveStatistics();
        resultMap.put("activeNum", userTotal - userNoActiveNum);
        resultMap.put("noActiveNum", userNoActiveNum);
        return resultMap;
    }


}
